import { Injectable } from '@angular/core';
import {Http,  Response} from "@angular/http";
import {Observable} from "rxjs/Observable";

import { GroupModal } from '../models/org-response-modal'

import "rxjs/add/operator/map";
import "rxjs/add/operator/catch";
import "rxjs/add/observable/throw";
import {environment} from "../../../environments/environment.local";

@Injectable()
export class GetOrginizationService {


  private api():string{
    if(environment.local){
      return "/assets/data/orgData.json";
    }
    return "powerplant/org";
  }
  private areaSet: GroupModal[]
  constructor(private http:Http) {
    this.getOrg().subscribe(
      data => this.areaSet = data
    )
  }



  getOrg():Observable<GroupModal[]>{
    return this.http.get(this.api())
      .map(this.extractData)
      .catch(this.handleError);
  }

  getLogoByName(name:string):Observable<any>{
    return new Observable<any>(observer => {
      this.http.get("assets/data/logoData.json")
        .map(res => res.json())
        .subscribe(
          data => {
            data.forEach(item => {
              if(item.name === name){
                observer.next(item.imgUrl)
                observer.complete()
              }
            })
          }
        )
    })
  }
  getOrgById(id:number):Observable<any>{
    return new Observable<any>(observer => {
      this.getOrg().subscribe(
        areaSet => {
          if(id == 0){
            observer.next(areaSet)
            observer.complete()
          }
          if(areaSet && areaSet instanceof Array) {
            for (let i = 0; i < areaSet.length; i++) {
              if (areaSet[i].id == id) {
                observer.next(areaSet[i])
                observer.complete()
              }
              let stationSet = areaSet[i].stationSet;
              if(stationSet && stationSet instanceof Array) {
                for (let j = 0; j < stationSet.length; j++) {
                  if (stationSet[j].id == id) {
                    observer.next(stationSet[j])
                    observer.complete()
                  }
                  var wtSet = stationSet[j].wtSet;
                  if(wtSet && wtSet instanceof Array) {
                    for (let k = 0; k < wtSet.length; k++) {
                      if (wtSet[k].wtcode == id) {
                        observer.next(wtSet[k])
                        observer.complete()
                      }
                    }
                  }
                }
              }
            }
          }
          observer.next(null)
          observer.complete()
        }
      )
    })
  }

  private extractData(res: Response) {
    let body = res.json();
    return body || {};
  }

  private handleError(error:Response|any) {
    let errmsg :string;
    if(error instanceof Response){
      const body = error.json()||{};
      const err = body.error || JSON.stringify(body);
      errmsg=`${error.status}--${error.statusText||''}${err}`;
    }
    else{
      errmsg=error.message?error.message:error.toString();
    }
    console.log(errmsg);
    return Observable.throw(errmsg);
  }
}
